package com.matt.repository.impl;

import com.matt.foundation.DO.LiveRoomDO;
import com.matt.foundation.DO.TeachAreaListDO;
import com.matt.foundation.DO.TeacherDO;
import com.matt.foundation.utils.ModelUtil;
import com.matt.repository.base.BaseNativeSqlRepository;
import com.matt.repository.base.ClassesTeachingAreaRepository;
import org.assertj.core.util.Lists;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2017/7/11
 * *******************************
 */
@Service
public class ClassesTeachingAreaRepositoryImpl extends BaseNativeSqlRepository implements ClassesTeachingAreaRepository {

    @Override
    public List<TeachAreaListDO> findByClassesId(String classesId) {
        String findTeacherSql=String.format("select r.id AS id, r.name AS name, r.cameraSize"
                + " from teaching_area r LEFT JOIN rel_classes_teaching_area rel ON r.id = rel.teachingAreaId where rel.classesId = '%s'", classesId);
        List<TeachAreaListDO> objectArrayList = sqlObjectList(findTeacherSql, TeachAreaListDO.class);
        return objectArrayList;
    }

    @Override
    public List<Object[]> findRoomsByClassesId(String classesId) {
        String findTeacherSql=String.format("select r.id AS id, r.name AS name, r.thumbUrl"
                + " from liveroom r LEFT JOIN teaching_area a on r.teachingAreaId = a.id left join rel_classes_teaching_area rel ON a.id = rel.teachingAreaId where rel.classesId = '%s'", classesId);
        List<Object[]> objectArrayList = sqlArrayList(findTeacherSql);
        return objectArrayList;
    }


    @Override
    public List<LiveRoomDO> findRoomsByChildIdsIn(List<String> childIds) {
        if (childIds.isEmpty())
            return Lists.newArrayList();
        String findSql=String.format("select DISTINCT r.aid as areaId,r.id AS id, r.name AS name, r.thumbUrl as thumbUrl, r.url as url, r.HDUrl as HDUrl, r.deviceSerial as deviceSerial, r.channelNo as channelNo,r.openTime, r.liveWay as livePlayWay "
                + " from liveroom r LEFT JOIN teaching_area a on r.teachingAreaId = a.id left join rel_classes_teaching_area rel ON a.id = rel.teachingAreaId  left join rel_classes_children rcc on rel.classesId = rcc.classesId where r.status = 1 " +
                " and rcc.childrenId in (%s)", ModelUtil.joinQuota(childIds));
        List<LiveRoomDO> objectArrayList = sqlObjectList(findSql, LiveRoomDO.class);
        return objectArrayList;
    }

    @Override
    public List<TeacherDO> findTeacherByClassId(String classId) {
        String findSql=String.format("SELECT ct.staffId AS id " +
                "FROM classes AS c LEFT JOIN rel_classes_teacher AS ct ON c.id = ct.classesId " +
                "WHERE c.id = '%s'", classId);
        List<TeacherDO> objectArrayList = sqlObjectList(findSql, TeacherDO.class);
        return objectArrayList;

    }
}
